package cn.wsh.basic.controller;

import cn.wsh.basic.dto.RegisterDto;
import cn.wsh.basic.exception.BusinessException;
import cn.wsh.basic.service.IVerifyCodeService;
import cn.wsh.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 发送验证码接口
 * 1.图形验证码
 * 2.手机注册验证码
 * 3.手机登录验证码
 * 4.邮箱注册验证码
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 发送图形验证码
     * @param key  浏览器唯一key
     * @return
     */
    @GetMapping("/image/{key}")
    public AjaxResult imageCode(@PathVariable("key") String key){
        try {
            //生成base64编码之后的图片string
            String base64Str = verifyCodeService.imageCode(key);
            //将base64编码之后的图片string
            return AjaxResult.me().setResultObj(base64Str);
        } catch (BusinessException e) {//捕获我们自己的业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！");
        }
    }

    /**
     * 发送短信验证码
     * @param dto 接收前端传递的参数对象
     * @return
     */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody RegisterDto dto){
        try {
            verifyCodeService.smsCode(dto);
            return AjaxResult.me();
        } catch (BusinessException e) {//捕获我们自己的业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！");
        }
    }



}
